﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;
using System.IO;

public partial class Admin_UserControl_ucAddCategories : System.Web.UI.UserControl
{
    TblCategoriesDAO CateDAO = new TblCategoriesDAO(Global.CONNECTION_STRING);
    public static int index;
    private static string sortExpression = null;
    private static string direction = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.ViewState["SelectedText"] = "All";
            load_Category();
            DetailCate();
            LoadGroup();
           
        }
       
    }

    public void load_Category()
    {
        grdCate.DataSource = CateDAO.GetAll();
        grdCate.DataBind();
    }

    private void LoadGroup()
    {
        drgroup.DataSource = CateDAO.GetGroup();
        drgroup.DataTextField = "GroupName";
        drgroup.DataValueField = "IDGroup";
        drgroup.DataBind();
    }

    // kiem tra co trung ten khong truoc khi insert
    private bool Check_CateExists()
    {
        bool bExists = false;
        try
        {

            bExists = CateDAO.check_cate(txtCateName.Text);
            System.Threading.Thread.Sleep(3000);
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect("~/Error.aspx", true);
        }
        return bExists;
    }

    protected void btAdd_Click(object sender, EventArgs e)
    {
        bool flag = false;
        try
        {
            TblCategories Cate = new TblCategories();
            string id = Request.QueryString["id"];

            Cate.CateName = txtCateName.Text;
            Cate.CateDescription = txtCateDes.Text;

            Cate.IDGroup = Convert.ToInt32(drgroup.SelectedValue);

            //string filename =   upload.FileName;

           // string path = Server.MapPath("~\\Admin\\images\\items\\");
              
                if (id == null || id == "")
                {
                    if (Check_CateExists())
                    {
                        lb_message.Text = "Category already existed.";
                        return;
                    }
                    else
                    {
                        
                        //if (File.Exists(path + "\\" + filename))
                        //{
                        //    lbmesage.Text = "The image file already exist. Please choose other file or change the file name to upload";
                        //    return;
                        //}
                        //upload.SaveAs(path + "\\" + filename);
                        
                        Cate.ImageURL = "";
                        
                       
                        Cate.CateID = 0;
                        CateDAO.Insert(Cate, 1);
                    }
                  
                }
                else
                {
                    //Cate.CateID = Global.ConvertToInt(id);
                    int id1 = Global.ConvertToInt(Request.QueryString["id"]);
                    TblCategories Category = CateDAO.GetCateByID(id1);
                    if (Category != null)
                    {
                       // string fname = Category.ImageURL;
                       // if (fname != null)
                       // {
                            //File.Delete(Server.MapPath("~\\Admin\\images\\items\\" + fname));
                        //}
                        // Update moi
                        Cate.CateID = id1;
                        Cate.CateName = txtCateName.Text;
                        Cate.CateDescription = txtCateDes.Text;
                        //Cate.ImageURL = filename;
                        Cate.ImageURL = "";
                        Cate.IDGroup = Convert.ToInt32(drgroup.SelectedValue);
                        //upload.SaveAs(Server.MapPath("~\\Admin\\images\\items\\" + filename));
                        CateDAO.Insert(Cate, 2);
                    }
                 
                }
                flag = true;
            }
            catch (Exception ex)
            {
                Session[Global.SESSION_ERROR] = ex;
                Response.Redirect(Global.ERROR_PAGE_ADMIN);
            }
            if (flag)
            {
                load_Category();
                // Panel1.Visible = false;
            }
       
    }
    protected void lb_addnew_Click(object sender, EventArgs e)
    {
      
        txtCateDes.Text = "";
        txtCateName.Text = "";
        Response.Redirect("~/Admin/ListCategories.aspx");
        
    }
    public void DetailCate()
    {
        LoadGroup();
          string id = Request.QueryString["id"];
          if (id == null)
          {
          
          }
          else
          {
              
              TblCategories Category = CateDAO.GetCateByID(Global.ConvertToInt(id));
              txtCateName.Text = Category.CateName;
              txtCateDes.Text = Category.CateDescription;
              drgroup.SelectedValue = Category.IDGroup.ToString();
            
             // upload.FileName = Server.MapPath("~\\Admin\\images\\items\\" + Category.ImageURL);

          }
    }

    // Phan trang cho Gridview
    protected void grdCate_PageIndexChanging1(object sender, GridViewPageEventArgs e)
    {
        grdCate.PageIndex = e.NewPageIndex;
        //LoadDriver();
        BindGrid(ViewState["SelectedText"].ToString());
    }

    private void BindGrid(string StartAlpha)
    {
        DataTable dt_dept = CateDAO.GetAll();
        DataView dv = new DataView(dt_dept); // dt_dept là table ch?a d? li?u c?a gridview
        dv.Sort = sortExpression + direction;
        grdCate.DataSource = dv;
        grdCate.DataBind();

    }

    // Sort cho Gridview

    
}
